package com.violet.controller.app;

import com.violet.domain.dto.app.AUserDto;
import com.violet.domain.result.Result;
import com.violet.domain.vo.app.AUserOrderVo;
import com.violet.domain.vo.app.AUserVo;
import com.violet.service.app.AUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/app/user")
@RequiredArgsConstructor
public class AUserController {

    private final AUserService service;

    @PostMapping("/login")
    public Result<AUserVo> login(@RequestBody AUserDto dto) {
        return Result.success(service.login(dto), "登录");
    }

    // 更新用户名
    @PutMapping("/rename")
    public Result<Boolean> rename(@RequestParam String name) {
        // 校验用户名
        if (name == null || name.length() < 2 || name.length() > 10) throw new RuntimeException("用户名长度必须在 2 到 10 之间");
        return Result.success(service.rename(name), "更新用户名");
    }

    // 更新用户头像
    @PostMapping("/avatar")
    public Result<String> avatar(@RequestBody MultipartFile file) {
        return Result.success(service.avatar(file), "更新头像");
    }

    // 拿到当前用户最近的三个订单数据
    @GetMapping("/recent")
    public Result<AUserOrderVo> recent() {
        return Result.success(service.recent(), "最近订单");
    }

}
